package com.example.dessertstoressm.controller;

import com.example.dessertstoressm.Service.AccountService;
import com.example.dessertstoressm.Service.CatalogService;
import com.example.dessertstoressm.domain.Category;
import com.example.dessertstoressm.domain.Item;
import com.example.dessertstoressm.domain.Product;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@SessionAttributes(value={"itemId","product","item"})
public class ItemFormController
{   @Resource
public CatalogService catalogService;
    @GetMapping ("itemForm")
public String itemForm(Model model, @RequestParam("itemId") String itemId,HttpServletRequest req)
{
   //CatalogService catalogService = null;
    //String itemId = req.getParameter("itemId");
    Item item = catalogService.getItem(itemId);
    Product product = item.getProduct();;

    model.addAttribute("itemId",itemId);
    model.addAttribute("product", product);
    model.addAttribute("item", item);

    String workingItemId = item.getItemId();

    HttpSession session = req.getSession();
    session.setAttribute("product", product);
    session.setAttribute("itemId", workingItemId);

    System.out.println(itemId);
    return "catalog/item";
}
}
/*
public class ItemFormController
{
    @Resource
    CatalogService catalogService;
    @RequestMapping("itemForm")
    public String itemForm(HttpServletRequest req)
 {
       String itemId = req.getParameter("itemId");
     System.out.println("传到item页面的");
      Item item = catalogService.getItem(itemId);
      Product product = item.getProduct();;
     HttpSession session = req.getSession();
       session.setAttribute("product", product);
       session.setAttribute("item", item);
      return "catalog/item";
   }
}*/


//public class ItemFormController
//{
//    @RequestMapping("itemForm")
//
//    public String itemForm()
//    {
//        return "catalog/item";
//    }
//}